import Vue from 'vue'
import store from '../index.js'
import routes from '@/router/index.js'
import { setInfo, getInfo, delInfo } from '@/utils/session.js'
import { login, loginOut } from '@/api/login'
import { getMenu } from '@/api/menu'
const state = {
    token: getInfo('token'),
    useState: getInfo('useState'),
    name: getInfo('name'),
    avatar: getInfo('avatar'),
    userId: getInfo('userId'),
    menu: getInfo('menu', 0),
    orgList: getInfo('orgList'),
    fhkzType: getInfo('fhkzType'),
    btnPermission: getInfo('btnPermission'),
    url: getInfo('url'),
    isGoBack: getInfo('isGoBack'),
    isAway: getInfo('isAway'),
    title: getInfo('title'),
    loginUser: getInfo('loginUser'),
    acceptFlag: getInfo('acceptFlag'),
    password: getInfo('password'),
}

const mutations = {
    SET_PASSWORD(state, password) {
        setInfo('password', password)
        state.password = password
    },
    SET_ACCEPTFLAG(state, acceptFlag) {
        setInfo('acceptFlag', acceptFlag)
        state.acceptFlag = acceptFlag
    },
    SET_TITLE(state, title) {
        setInfo('title', title)
        state.title = title
    },
    SET_ISGOBACK(state, isGoBack) {
        setInfo('isGoBack', isGoBack)
        state.isGoBack = isGoBack
    },
    SET_URL: (state, url) => {
        setInfo('url', url)
        state.url = url
    },
    SET_TOKEN: (state, token) => {
        setInfo('token', token)
        state.token = token
    },
    SET_USERSTETE: (state, useState) => {
        setInfo('useState', useState)
        state.useState = useState
    },
    SET_NAME: (state, name) => {
        setInfo('name', name)
        state.name = name
    },
    SET_AVATER: (state, avatar) => {
        setInfo('avatar', avatar)
        state.avatar = avatar
    },
    SET_USERID: (state, userId) => {
        setInfo('userId', userId)
        state.userId = userId
    },
    SET_MENU: (state, menu) => {
        setInfo('menu', menu)
        state.menu = menu
    },
    SET_ORGLIST: (state, orgList) => {
        setInfo('orgList', orgList)
        state.orgList = orgList
    },
    SET_FHKZTYPE: (state, fhkzType) => {
        setInfo('fhkzType', fhkzType)
        state.fhkzType = fhkzType
    },
    SET_BTNPERMISSION: (state, btnPermission) => {
        setInfo('btnPermission', btnPermission)
        state.btnPermission = btnPermission
    },
    SET_ISAWAY: (state, isAway) => {
        setInfo('isAway', isAway)
        state.isAway = isAway
    },
    SET_lOGINUSER: (state, loginUser) => {
        setInfo('loginUser', loginUser)
        state.loginUser = loginUser
    }
}

const actions = {
    login({ commit }, useInfo) {
        const { userName, password } = useInfo
        return new Promise((resolve, reject) => {
            login({
                account: userName,
                password: password
            }).then(res => {
                if (res.code == 200) {

                    commit('SET_PASSWORD', password)
                    const {
                        name,
                        id,
                        department,
                        headAddress,
                        authoritys,
                        acceptFlag
                    } = res.data.loginUser
                        // if (authoritys && authoritys.length > 0) {
                        //     authoritys.map(item => {
                        //         item["AUTHORITY"] = item["AUTHORITY"].split(',')
                        //         if (item["AUTHORITY"].length > 0) {
                        //             var child = item["AUTHORITY"]
                        //             child.map(c => {
                        //                 auths.push(c)
                        //             })
                        //         }
                        //     })
                        // }
                    commit('SET_lOGINUSER', res.data.loginUser)
                    commit('SET_TOKEN', res.data.token)
                    commit('SET_USERSTETE', true)
                    commit('SET_NAME', name)
                    commit('SET_USERID', id)
                    commit('SET_AVATER', headAddress)
                    commit('SET_ORGLIST', department)
                    commit('SET_FHKZTYPE', name)
                    commit('SET_ISGOBACK', false)
                    commit('SET_ISAWAY', true)
                    commit('SET_TITLE', res.data.title)
                    commit('SET_URL', null)
                    commit('SET_ACCEPTFLAG', acceptFlag)
                        // 菜单
                    var menuList = {
                        BackList: [],
                        systemList: []
                    }
                    getMenu({}).then((res) => {
                        var data = res.data
                        var auths = []
                        if (res.data && res.data.length > 0) {
                            data.map((item) => {
                                if (item.menuUrl == '/websiteBack') {
                                    item.children.map((child) => {
                                        if (child.menuUrl == '/backManage') {
                                            // 后台管理
                                            menuList.BackList = child
                                            var fn = (arr) => {
                                                arr.map((item) => {
                                                    item.label = item.menuName
                                                    item.name = item.menuUrl.slice(1)
                                                    if (item.children) {
                                                        item.child = item.children
                                                        fn(item.child)
                                                    }
                                                })
                                            }
                                            fn(menuList.BackList.children)
                                            commit('SET_MENU', menuList)
                                        } else {
                                            // 系统管理
                                            menuList.systemList = child
                                            var fn = (arr) => {
                                                arr.map((item) => {
                                                    item.label = item.menuName
                                                    item.name = item.menuUrl.slice(1)
                                                    if (item.children) {
                                                        item.child = item.children
                                                        fn(item.child)
                                                    }
                                                })
                                            }
                                            fn(menuList.systemList.children)
                                            commit('SET_MENU', menuList)
                                        }
                                    })
                                } else if (item.menuUrl == '/PlatformChange') {
                                    menuList.PlatformChange = item.children
                                    commit('SET_MENU', menuList)
                                }
                            })
                            var fn = (arr) => {
                                arr.map((item) => {
                                    if (item.authority) {
                                        auths.push(item.authority)
                                    }
                                    if (item.children) {
                                        fn(item.children)
                                    }
                                })
                            }
                            fn(data)
                            commit('SET_BTNPERMISSION', auths)
                        } else {
                            commit('SET_MENU', [])
                            commit('SET_BTNPERMISSION', [])
                        }
                    })
                }
                resolve(res)
            }).catch(error => {
                reject(error)
            })
        })
    },
    logout({ commit, state }) {
        return new Promise((resolve, reject) => {
            loginOut({
                token: state.token
            }).then(res => {
                commit('SET_USERSTETE', false)
                commit('SET_ISGOBACK', false)
                sessionStorage.clear()
                resolve(res)
            })
        })
    },
    authUrl({ commit }, url) {
        commit('SET_URL', url)
    },
    useState({ commit }, useState) {
        commit('SET_USERSTETE', useState)
    },
    isGoBack({ commit }, isGoBack) {
        commit('SET_ISGOBACK', isGoBack)
    }
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}